package com.baibei.ebec.home.shipment;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baibei.ebec.home.R;
import com.baibei.ebec.home.index.holder.JewelryHeaderViewHolder;
import com.baibei.ebec.home.index.holder.ShipmentProductViewHolder;
import com.baibei.model.ProductInfo;
import com.baibei.model.ShipmentInfo;
import com.bumptech.glide.Glide;
import com.rae.swift.Rx;

import java.util.List;

import static com.baibei.ebec.home.shipment.ShipmentAdapter.ViewType.EMPTY;
import static com.baibei.ebec.home.shipment.ShipmentAdapter.ViewType.JEWELRY;
import static com.baibei.ebec.home.shipment.ShipmentAdapter.ViewType.PRODUCTS;

/**
 * 船适配器
 * Created by shenminjie on 2017/8/7.
 * mail:shenminjie@100bei.com
 */

class ShipmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    /**
     * 三大数据
     */
    private List<ShipmentInfo> mSources; // 数据源
    private SparseArray<ShipmentInfo> mShipInfos;
    private SparseArray<ProductInfo> mProductsInfos;
    private int totalCount;
    private OnAdapterListener mListener;

    ShipmentAdapter(List<ShipmentInfo> mInfos, OnAdapterListener listener) {
        this.mSources = mInfos;
        mShipInfos = new SparseArray<>();
        mProductsInfos = new SparseArray<>();
        mListener = listener;
        updateInfoPosition();
    }

    /**
     * 视图枚举
     */
    enum ViewType {
        JEWELRY, PRODUCTS, EMPTY
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == JEWELRY.ordinal()) {
            return new JewelryHeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jewelry_header, parent, false));
        } else if (viewType == PRODUCTS.ordinal()) {
            return new ShipmentProductViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shipment_product, parent, false));
        } else if (viewType == EMPTY.ordinal()) {
            return new RecyclerView.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shipment_empty_view, parent, false)) {
            };
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof JewelryHeaderViewHolder) {
            JewelryHeaderViewHolder viewHolder = (JewelryHeaderViewHolder) holder;
            final ShipmentInfo info = mShipInfos.get(position);
//            Area area = Area.valueOf(info.getArea());
            viewHolder.tvName.setText(info.getName());
            viewHolder.tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            viewHolder.tvTime.setText(info.getSaleTips());
        }
        if (holder instanceof ShipmentProductViewHolder) {
            ShipmentProductViewHolder viewHolder = (ShipmentProductViewHolder) holder;
            final ProductInfo info = mProductsInfos.get(position);

            viewHolder.getTitleView().setText(info.getName());
            viewHolder.getNumberView().setText(info.getRepertoryStatus()); //充足与否
            viewHolder.getPriceView().setText(String.format("¥%s", info.getLastPrice() <= 0 ? "暂无" : Rx.formatPrice(info.getLastPrice())));

            Glide.with(holder.itemView.getContext())
                    .load(info.getProductPic())
                    .into(viewHolder.getProductImageView());
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onProctorInfoClickListener(mShipInfos.get(info.getShipmentPosition()), info);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (totalCount == 0) {
            return EMPTY.ordinal();
        }
        if (mShipInfos.get(position) != null) {
            return JEWELRY.ordinal();
        }
        if (mProductsInfos.get(position) != null) {
            return ViewType.PRODUCTS.ordinal();
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        if (totalCount == 0) {
            return 1;
        }
        return totalCount;
    }

    /**
     * 更新数据
     */
    void updateInfoPosition() {
        mShipInfos.clear();
        mProductsInfos.clear();

        int size = 0;
        for (int i = 0; i < mSources.size(); i++) {
            size++;
            ShipmentInfo info = mSources.get(i);
            //记录坐标
            int shipmentPosition = size - 1;
            mShipInfos.put(shipmentPosition, info);
            for (int j = 0; j < info.getProducts().size(); j++) {
                size++;
                ProductInfo productInfo = info.getProducts().get(j);
                productInfo.setShipmentPosition(shipmentPosition);
                //设置基础数据
                //记录坐标
                mProductsInfos.put(size - 1, productInfo);
            }
        }
        totalCount = size;
    }

    /**
     * 回调
     */
    interface OnAdapterListener {
        /**
         * callback
         *
         * @param info info
         */
        void onShipmentInfoClickListener(ShipmentInfo info);

        /**
         * 商品点击事件
         *
         * @param info info
         */
        void onProctorInfoClickListener(ShipmentInfo shipmentInfo, ProductInfo info);
    }
}
